<div class="container-fluid">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">储位移位</h3></div>
            <div class="panel-body">
                    <form id="remove-form">
                    <div class="col-md-6 form-inline">
                        <label for="">SKU</label> 
                        <input class="form-control add-space" name="sku" type="text">
                        <input class="btn btn-primary btn-search-from" type="button" value="查询">
                        <div class="clearfix"></div>
                        <table class="table table-striped table-hover table-from" cellspacing="0" width="100%">
                            <thead>
                            <tr>
                                <th><input type="checkbox" id="checkAll" /></th>
                                <th>站点</th>
                                <th>储位</th>
                                <th>可用数量</th>
                                <th>移动数量</th>
                                <th>公司主体</th>
                                <th>是否退税</th>
                            </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                    <div class="col-md-6 form-inline">
                        <label for="">到达储位</label> 
                        <input class="form-control add-space" name="to_storage" type="text">
                        <input class="btn btn-primary btn-search-to" type="button" value="查询">
                        <div class="clearfix"></div>
                        <table class="table table-striped table-hover table-to" cellspacing="0" width="100%">
                            <thead>
                            <tr>
                                <th>站点</th>
                                <th>SKU</th>
                                <th>数量</th>
                                <th>公司主体</th>
                                <th>是否退税</th>
                            </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                    </div>
                    </form>
                    <div class="col-md-12">
                        <button class="btn btn-primary pull-right btn-sure-move">确定移位</button>
                    </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //原始储位查询
    $('.btn-search-from').click(function() {
        var sku = $.trim($('input[name="sku"]').val());
        if (sku == '') {
            layer.msg('请输入需要查询的SKU！', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: 'Warehouse/Inventory/move_storage',
            data: {sku: sku},
            dataType: "json",
            success: function (result) {
                $('.table-from').find('tbody').html('');
                if (!result.status) {
                    layer.msg(result.info, {icon: 5});
                    return false;
                }

                var data = result.info;
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<tr style="height:50px">';
                    html += '<td>';
                    // html += '<input type="hidden" name="site[]" value="'+data[i]['site_id']+'" />';
                    // html += '<input type="hidden" name="storage_position[]" value="'+data[i]['storage_position']+'" />';
                    // html += '<input type="hidden" name="enterprise_dominant[]" value="'+data[i]['enterprise_dominant']+'" />';
                    // html += '<input type="hidden" name="export_tax_rebate[]" value="'+data[i]['export_tax_rebate']+'" />';
                    var jsonStr = '{"site_id": "'+data[i]['site_id']+'", "storage_position": "'+data[i]['storage_position']+'", "enterprise_dominant": "'+data[i]['enterprise_dominant']+'", "export_tax_rebate": "'+data[i]['export_tax_rebate']+'"}';
                    html += "<input type='checkbox' class='switch' name='check[]' value='"+ jsonStr +"' />";
                    html += '</td>';
                    html += '<td>' + data[i]['site_name'] + '</td>';
                    html += '<td>' + data[i]['storage_position'] + '</td>';
                    html += '<td>' + data[i]['available_quantity'] + '</td>';
                    html += '<td>';
                    html += '<input type="number" class="form-control w80 input-quantity" value="' + data[i]['available_quantity'] + '" max="' + data[i]['available_quantity'] + '" min="1" onblur="setValue(this)" />';
                    html += '</td>';
                    html += '<td>' + data[i]['company_name'] + '</td>';
                    html += '<td>' + data[i]['export_tax_rebate_text'] + '</td>';
                    html += '</tr>';
                }
                $('.table-from').find('tbody').append(html);
            }
        });        
    });

    function setValue(obj) {
        var val = $.trim($(obj).val());
        if (parseInt(val) > parseInt($(obj).attr('max'))) {
            layer.msg('移动数量不能大于可用数量', {icon: 5});
            $(obj).val($(obj).attr('max'));
            return false;
        }

        if (parseInt(val) < parseInt($(obj).attr('min'))) {
            layer.msg('移动数量不能小于1', {icon: 5});
            $(obj).val($(obj).attr('min'));
            return false;
        }
    }


    //到达储位查询
    $('.btn-search-to').click(function() {
        var storage = $.trim($('input[name="to_storage"]').val());
        if (storage == '') {
            layer.msg('请输入需要查询的储位！', {icon: 5});
            return false;
        }

        $.ajax({
            type: "post",
            url: 'Warehouse/Inventory/move_storage',
            data: {storage: storage},
            dataType: "json",
            success: function (result) {
                $('.table-to').find('tbody').html('');
                if (!result.status) {
                    layer.msg(result.info, {icon: 5});
                    return false;
                }

                var data = result.info;
                var html = '';
                for (var i = 0; i < data.length; i++) {
                    html += '<tr style="height:50px">';
                    html += '<td>' + data[i]['site_name'] + '</td>';
                    html += '<td>' + data[i]['sku'] + '</td>';
                    html += '<td>' + data[i]['available_quantity'] + '</td>';
                    html += '<td>' + data[i]['company_name'] + '</td>';
                    html += '<td>' + data[i]['export_tax_rebate_text'] + '</td>';
                    html += '</tr>';
                }
                $('.table-to').find('tbody').append(html);
            }
        });
    });
    
    //全选
    $('#checkAll').click(function() {
        $("input[name='check[]']").prop("checked", $(this).prop("checked"));
        
        $("input[name='check[]']").each(function() {
            if ($(this).is(':checked')) {
                $(this).parent().parent().find('.input-quantity').attr('name', 'available_quantity[]');
            } else {
                $(this).parent().parent().find('.input-quantity').removeAttr('name');
            }
        });
    });

    //判断是否选中
    $(".table-from").on('click', '.switch', function() {
        if ($(this).is(':checked')) {
            $(this).parent().parent().find('.input-quantity').attr('name', 'available_quantity[]');
        } else {
            $(this).parent().parent().find('.input-quantity').removeAttr('name');
        }
    });

    //点击确定移位
    $('.btn-sure-move').click(function() {
        var sku = $.trim($('input[name="sku"]').val());
        var toStorage = $.trim($('input[name="to_storage"]').val());

        if (sku == '') {
            layer.msg('请输入SKU！', {icon: 5});
            return false;
        }

        if (toStorage == '') {
            layer.msg('请输入到达储位！', {icon: 5});
            return false;
        }

        var checkValue = []; 
        $('input[name="check[]"]:checked').each(function(){ 
            checkValue.push($(this).val()); 
        }); 

        if (checkValue.length == 0) {
            layer.msg('请至少勾选一个需要转移数量的站点储位！', {icon: 5});
            return false;
        }

        if (layer.confirm('您确定要移位吗？', function () {
            if ($('.layui-layer-btn0').hasClass('btn-disabled')) {
                return false;
            }

            $('.layui-layer-btn0').addClass('btn-disabled').html('移位中...');
            $.ajax({
                type: "post",
                url: 'Warehouse/Inventory/do_move',
                data: $('#remove-form').serialize(),
                dataType: "json",
                success: function (result) {
                    if (!result.status) {
                        layer.msg(result.info, {icon: 5});
                        return false;
                    }

                    layer.msg(result.info, {icon: 6});
                    setTimeout(function () {
                        $('.btn-search-from').click();
                        $('.btn-search-to').click();
                    }, 2000);
                    $('.layui-layer-btn0').removeClass('btn-disabled').html('确定');
                }
            });
        }));
    });
</script>
